package com.project.service.impl;


import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl extends BaseService implements IUserService {
    @Override
    public UserBean login(String userName, String pwd) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        boolean flag = mapper.checkName(userName);
        session.close();
        if(flag == false){
            return null;
        }

        SqlSession session2 = this.getSession();
        IUserMapper mapper2 = session2.getMapper(IUserMapper.class);
        UserBean user = mapper2.login(userName,pwd);
        session2.close();
        return user;
    }

    @Override
    public String changePsw(int code, String oldPsw, String newPsw, String newPsw2) {
        UserBean user = this.findById(code);
        String str = "";
        if(oldPsw.equals(user.getPsw()) == false){
            str+="原密码不正确\n";
        }
        if(newPsw.equals(newPsw2) == false){
            str+="两次密码不一致";
        }
        if(str.length() != 0){
            return str;
        }

        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.updatePwd(code,newPsw2);
        session.commit();
        session.close();
        return "修改成功";
    }

    @Override
    public void add(UserBean user) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.addUser(user);
        session.commit();
        session.close();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.delUser(id);
        session.commit();
        session.close();
    }

    @Override
    public UserBean findById(int id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean user = mapper.findById(id);
        session.commit();
        session.close();
        return user;
    }

    @Override
    public List<UserBean> findAll() {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> list = mapper.findAll();
        session.commit();
        session.close();
        return list;
    }

    @Override
    public void changeGrade(int id, String grade) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.updateRank(id,grade);
        session.commit();
        session.close();
    }

    public static void main(String[] args) {
        IUserService us = new UserServiceImpl();
//        System.out.println(us.changePsw(3, "333", "555", "555"));
//        System.out.println(us.login("jack", "444"));
//        us.add(new UserBean("bob", "我我我", "超级管理员"));
//        us.del(5);
//        System.out.println(us.findAll());
//        System.out.println(us.findById(4));
//        us.changeGrade(2,"班主任");
    }
}
